package net.skyedu.service.administrator;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.skyedu.bean.Appeal;
import net.skyedu.bean.Attendance;
import net.skyedu.dao.UserInfoDao;


public class Approve extends HttpServlet {

	private static final long serialVersionUID = -1232045303234538763L;

	/**
	 * Constructor of the object.
	 */
	public Approve() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@SuppressWarnings("unused")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		int attId=Integer.parseInt(request.getParameter("appID"));
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		int sta=Integer.parseInt(request.getParameter("sta"));
		
		UserInfoDao ud=new UserInfoDao();
		Appeal ap=ud.getRecip(attId);
		int recipientid=ap.getRecipientid();
		int appattid=ap.getAppattid();
		Attendance att=new Attendance();
		Appeal app=new Appeal();
		att.setAttid(appattid);
		app.setAppid(attId);
		int num=0;
		if(sta==1){
			ud.upConfirm(att);
			app.setType(1);
		}
		if(sta==2){
			att.setAttconfirm(5);
			ud.updateConfirm(att);
			app.setType(2);
		}
		num = ud.updateAppType(app);
		if(num > 0 ){
			request.getRequestDispatcher("Accepts?rid="+recipientid+"&startTime="+startTime+"&endTime="+endTime+"").forward(request, response);
		}else{
			out.print("<script type='text/javascript'>alert('fail!');history.go(-1);</script>");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
